package cn.stylefeng.overall.modular.config.controller;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.common.constant.factory.PageFactory;
import cn.stylefeng.guns.core.common.page.PageInfoBT;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.overall.modular.config.model.StandardEquType;
import cn.stylefeng.overall.modular.config.service.IStandardEquTypeService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 设备类型管理控制器
 *
 * @author dusd
 * @Date 2019-04-25 20:56:20
 */
@Controller
@RequestMapping("/standardEquType")
public class StandardEquTypeController extends BaseController {

    private String PREFIX = "/config/standardEquType/";

    @Autowired
    private IStandardEquTypeService standardEquTypeService;

    /**
     * 跳转到设备类型管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "standardEquType.html";
    }

    /**
     * 跳转到添加设备类型管理
     */
    @RequestMapping("/standardEquType_add")
    public String standardEquTypeAdd() {
        return PREFIX + "standardEquType_add.html";
    }

    /**
     * 跳转到修改设备类型管理
     */
    @RequestMapping("/standardEquType_update/{standardEquTypeId}")
    public String standardEquTypeUpdate(@PathVariable String standardEquTypeId, Model model) {
        StandardEquType standardEquType = standardEquTypeService.selectById(standardEquTypeId);
        model.addAttribute("item", standardEquType);
        LogObjectHolder.me().set(standardEquType);
        return PREFIX + "standardEquType_edit.html";
    }

    /**
     * 获取设备类型管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        Page<StandardEquType> page = new PageFactory<StandardEquType>().defaultPage();
        Wrapper<StandardEquType> wrapper = (Wrapper<StandardEquType>) SqlHelper.fillWrapper(page, null);
        // sql 条件 start
        if (StrUtil.isNotBlank(condition)) {
            wrapper.like("ST_EQU_TYPE_NAME", condition.trim());
        }

        wrapper.eq("DEL_FLAG", "N");
        // sql 条件 end
        page = standardEquTypeService.selectPage(page, wrapper);
        return new PageInfoBT<>(page);
    }

    /**
     * 新增设备类型管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@Validated StandardEquType standardEquType) {
        //code 重复验证 start
        Wrapper<StandardEquType> wrapper = new EntityWrapper<>();
        wrapper.eq("ST_EQU_TYPE_CODE", standardEquType.getStEquTypeCode());
        wrapper.eq("DEL_FLAG","N");
        Object standardEquType1 = standardEquTypeService.selectObj(wrapper);
        if(standardEquType1 != null) {
            return new ErrorResponseData("编码不能重复");
        }
        //code 重复验证 end
        //insert 赋值
        super.initInsertObject(standardEquType);
        standardEquTypeService.insert(standardEquType);
        return SUCCESS_TIP;
    }

    /**
     * 删除设备类型管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String standardEquTypeId) {
        StandardEquType standardEquType = standardEquTypeService.selectById(standardEquTypeId);
        standardEquType.setDelFlag("Y");
        standardEquTypeService.updateById(standardEquType);
        return SUCCESS_TIP;
    }

    /**
     * 修改设备类型管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Validated StandardEquType standardEquType) {
        //code 重复验证 start
        Wrapper<StandardEquType> wrapper = new EntityWrapper<>();
        wrapper.eq("ST_EQU_TYPE_CODE", standardEquType.getStEquTypeCode());
        wrapper.eq("DEL_FLAG","N");
        wrapper.ne("ID", standardEquType.getId());
        Object standardEquType1 = standardEquTypeService.selectObj(wrapper);
        if(standardEquType1 != null) {
            return new ErrorResponseData("编码不能重复");
        }
        //code 重复验证 end
        //update 赋值
        super.initUpdateObject(standardEquType);
        standardEquTypeService.updateById(standardEquType);
        return SUCCESS_TIP;
    }

    /**
     * 设备类型管理详情
     */
    @RequestMapping(value = "/detail/{standardEquTypeId}")
    @ResponseBody
    public Object detail(@PathVariable("standardEquTypeId") String standardEquTypeId) {
        return standardEquTypeService.selectById(standardEquTypeId);
    }
}
